﻿using Assessment.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_Gauge_SelectGauge : BasePage_Index
{
    #region 属性
    public int MedicalRecordId
    {
        get { return Request.QueryString["MedicalRecordId"] == "" ? 0 : Convert.ToInt32(Request.QueryString["MedicalRecordId"]); }
    }
    public int GaugeTypeId
    {
        get { return Request.QueryString["GaugeTypeId"] == "" ? 0 : Convert.ToInt32(Request.QueryString["GaugeTypeId"]); }
    }

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            base.SetBtnGotoBackUrl("SelectGaugeType.aspx?MedicalRecordId=" + MedicalRecordId);
            BindRepeaterList();
        }
    }

    #region 绑定数据
    private void BindRepeaterList()
    {
        PaginateCtl.PageSize = base.CurrentSession.PageSize;
        int pageIndex = PaginateCtl.CurrentPageIndex;
        int pageSize = PaginateCtl.PageSize;

        UtilityBLL ubll = new UtilityBLL();

        int rCount = 0;
        string swhere = "Valid=1 and GaugeTypeId=" + GaugeTypeId;
        DataTable dt = ubll.GetListByPage("T_Gauge", "*", "GaugeId", swhere, pageSize, pageIndex, 0, out rCount);

        //绑定数据源
        rpt_List.DataSource = dt;
        //返回记录条数
        PaginateCtl.RecordCount = rCount;

        //设置记录总数的显示文本(此方法必须在设置PaginateCtl.RecordCount之后调用)
        TextUtility.SetPaginateCtlRecoText(PaginateCtl);
        rpt_List.DataBind();
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PaginateCtl_PageChanged(object sender, EventArgs e)
    {
        BindRepeaterList();
    }
    #endregion


    #region 保存按钮
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        Dictionary<int, string> dicSelectedGauge = Session["dicSelectedGauge"] == null ? new Dictionary<int, string>() : (Dictionary<int, string>)Session["dicSelectedGauge"];
        foreach (RepeaterItem item in this.rpt_List.Items)
        {
            CheckBox cb_Select = (CheckBox)item.FindControl("cb_Select");
            HiddenField hf_GaugeId = (HiddenField)item.FindControl("hf_GaugeId");
            HiddenField hf_Title = (HiddenField)item.FindControl("hf_Title");
            int gaugeid = Convert.ToInt32(hf_GaugeId.Value);
            string title = hf_Title.Value;
            if (cb_Select.Checked)
            {
                if (!dicSelectedGauge.ContainsKey(gaugeid))
                {
                    dicSelectedGauge.Add(gaugeid, title);
                }
            }
            else
            {
                if (dicSelectedGauge.ContainsKey(gaugeid))
                {
                    dicSelectedGauge.Remove(gaugeid);
                }
            }
        }
        Session["dicSelectedGauge"] = dicSelectedGauge;

        Response.Redirect("GaugeConfirm.aspx?MedicalRecordId=" + MedicalRecordId);
    }
    #endregion

    #region RepeaterList操作
    protected void rpt_List_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void rpt_List_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Dictionary<int, string> dicSelectedGauge = Session["dicSelectedGauge"] == null ? new Dictionary<int, string>() : (Dictionary<int, string>)Session["dicSelectedGauge"];

            CheckBox cb_Select = (CheckBox)e.Item.FindControl("cb_Select");
            HiddenField hf_GaugeId = (HiddenField)e.Item.FindControl("hf_GaugeId");
            int gaugeid = Convert.ToInt32(hf_GaugeId.Value);

            cb_Select.Checked = dicSelectedGauge.ContainsKey(gaugeid);
        }
    }
    #endregion
}